home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / python / maclibnx.lha / getwd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-14  |  3.3 KB  |  107 lines

  1. /* Get full pathname of current working directory.  The pathname is
  2.    copied to the parameter array 'cwd', and a pointer to this array
  3.    is also returned as function result.  If an error occurred, however,
  4.    the return value is NULL but 'cwd' is filled with an error message.
  5.    
  6.    BUG: expect spectacular crashes when called from a directory whose
  7.    path would be over MAXPATH bytes long (files in such directories are
  8.    not reachable by full pathname).
  9.    
  10.    Starting with the dir ID returned by PBHGetVol, we do successive
  11.    PBGetCatInfo's to get a component of the path until we reach the
  12.    root (recognized by a dir ID of 2).  We move up along the path
  13.    using the dir ID of the parent directory returned by PBGetCatInfo.
  14.    
  15.    Then we catenate the components found in reverse order with the volume
  16.    name (already gotten from PBHGetVol), with intervening and trailing
  17.    colons
  18.    
  19.    The code works correctly on MFS disks (where it always returns the
  20.    volume name) by simply skipping the PBGetCatinfo calls in that case.
  21.    There is a 'bug' in PBGetCatInfo when called for an MFS disk (with
  22.    HFS running): it then seems to call PBHGetVInfo, which returns a
  23.    larger parameter block.  But we won't run into this problem because
  24.    we never call PBGetCatInfo for the root (assuming that PBHGetVol
  25.    still sets the root ID in this case).
  26.  
  27.    Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
  28. */
  29.  
  30. #include "macdefs.h"
  31.  
  32. #define ROOTID 2 /* Root directory ID */
  33.  
  34. char *
  35. getwd(cwd)
  36.     char *cwd;
  37. {
  38.     /* Universal parameter block. */
  39.     union {
  40. #ifdef THINK_C
  41.         HFileInfo f;
  42.         DirInfo d;
  43.         WDPBRec w;
  44. #else /* MPW */
  45.         struct HFileInfo f;
  46.         struct DirInfo d;
  47.         struct WDPBRec w;
  48. #endif
  49.     } pb;
  50.     char buf[MAXPATH]; /* Buffer to store the name components */
  51.     char *ecwd, *ebuf; /* Pointers to end of used part of cwd and buf */
  52.     int err; /* Error code of last I/O call */
  53.     
  54.     /* First, get the default volume name and working directory ID. */
  55.     
  56.     pb.w.ioNamePtr= (unsigned char *)cwd;
  57.     err= PBHGetVol(&pb.w, FALSE);
  58.     if (err != noErr) {
  59.         sprintf(cwd, "I/O error %d in PBHGetVol", err);
  60.         return NULL;
  61.     }
  62.     ecwd= strchr(p2cstr((unsigned char*)cwd), EOS);
  63.     ebuf= buf;
  64.     *ebuf = EOS;
  65.     
  66.     /* Next, if at least we're running HFS, walk up the path. */
  67.     
  68.     if (hfsrunning()) {
  69.         long dirid= pb.w.ioWDDirID;
  70.         pb.d.ioVRefNum= pb.w.ioWDVRefNum;
  71.         while (dirid != ROOTID) {
  72.             pb.d.ioNamePtr= (unsigned char *) ++ebuf;
  73.             pb.d.ioFDirIndex= -1;
  74.             pb.d.ioDrDirID= dirid;
  75.             err= PBGetCatInfo((CInfoPBPtr)&pb.d, FALSE);
  76.             if (err != noErr) {
  77.                 sprintf(cwd, "I/O error %d in PBGetCatInfo", err);
  78.                 return NULL;
  79.             }
  80.             dirid= pb.d.ioDrParID;
  81.             ebuf += strlen(p2cstr((unsigned char *)ebuf));
  82.             /* Should check for buf overflow */
  83.         }
  84.     }
  85.     
  86.     /* Finally, reverse the list of components and append it to cwd.
  87.        Ebuf points at the EOS after last component,
  88.        and there is an EOS before the first component.
  89.        If there are no components, ebuf equals buf (but there
  90.        is still an EOS where it points).
  91.        Ecwd points at the EOS after the path built up so far,
  92.        initially the volume name.
  93.        We break out of the loop in the middle, thus
  94.        appending a colon at the end in all cases. */
  95.     
  96.     for (;;) {
  97.         *ecwd++ = ':';
  98.         if (ebuf == buf)
  99.             break;
  100.         do { } while (*--ebuf != EOS); /* Find component start */
  101.         strcpy(ecwd, ebuf+1);
  102.         ecwd= strchr(ecwd, EOS);
  103.     }
  104.     *ecwd= EOS;
  105.     return cwd;
  106. }
  107.